//
//  Detail.m
//
//  Created by TuanNT on 5/4/14
//  Copyright (c) 2014 Fsoft. All rights reserved.
//

#import "Detail.h"
#import "Files.h"
#import "PropertyInfo.h"
#import "Map.h"


NSString *const kDetailAuction = @"Auction";
NSString *const kDetailDescription = @"Description";
NSString *const kDetailID = @"ID";
NSString *const kDetailSub = @"Sub";
NSString *const kDetailListedDate = @"ListedDate";
NSString *const kDetailContract = @"Contract";
NSString *const kDetailAddress = @"Address";
NSString *const kDetailMain = @"Main";
NSString *const kDetailFiles = @"Files";
NSString *const kDetailPrice = @"Price";
NSString *const kDetailTitle = @"Title";
NSString *const kDetailPropertyInfo = @"PropertyInfo";
NSString *const kDetailMap = @"Map";


@interface Detail ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation Detail

@synthesize auction = _auction;
@synthesize internalBaseClassDescription = _internalBaseClassDescription;
@synthesize iDProperty = _iDProperty;
@synthesize sub = _sub;
@synthesize listedDate = _listedDate;
@synthesize contract = _contract;
@synthesize address = _address;
@synthesize main = _main;
@synthesize files = _files;
@synthesize price = _price;
@synthesize title = _title;
@synthesize propertyInfo = _propertyInfo;
@synthesize map = _map;


+ (Detail *)modelObjectWithDictionary:(NSDictionary *)dict
{
    Detail *instance = [[Detail alloc] initWithDictionary:dict];
    return instance;
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.auction = [self objectOrNilForKey:kDetailAuction fromDictionary:dict];
            self.internalBaseClassDescription = [self objectOrNilForKey:kDetailDescription fromDictionary:dict];
            self.iDProperty = [self objectOrNilForKey:kDetailID fromDictionary:dict];
            self.sub = [self objectOrNilForKey:kDetailSub fromDictionary:dict];
            self.listedDate = [self objectOrNilForKey:kDetailListedDate fromDictionary:dict];
            self.contract = [self objectOrNilForKey:kDetailContract fromDictionary:dict];
            self.address = [self objectOrNilForKey:kDetailAddress fromDictionary:dict];
            self.main = [self objectOrNilForKey:kDetailMain fromDictionary:dict];
    NSObject *receivedFiles = [dict objectForKey:kDetailFiles];
    NSMutableArray *parsedFiles = [NSMutableArray array];
    if ([receivedFiles isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedFiles) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedFiles addObject:[Files modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedFiles isKindOfClass:[NSDictionary class]]) {
       [parsedFiles addObject:[Files modelObjectWithDictionary:(NSDictionary *)receivedFiles]];
    }

    self.files = [NSArray arrayWithArray:parsedFiles];
            self.price = [self objectOrNilForKey:kDetailPrice fromDictionary:dict];
            self.title = [self objectOrNilForKey:kDetailTitle fromDictionary:dict];
    NSObject *receivedPropertyInfo = [dict objectForKey:kDetailPropertyInfo];
    NSMutableArray *parsedPropertyInfo = [NSMutableArray array];
    if ([receivedPropertyInfo isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedPropertyInfo) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedPropertyInfo addObject:[PropertyInfo modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedPropertyInfo isKindOfClass:[NSDictionary class]]) {
       [parsedPropertyInfo addObject:[PropertyInfo modelObjectWithDictionary:(NSDictionary *)receivedPropertyInfo]];
    }

    self.propertyInfo = [NSArray arrayWithArray:parsedPropertyInfo];
            self.map = [Map modelObjectWithDictionary:[dict objectForKey:kDetailMap]];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.auction forKey:kDetailAuction];
    [mutableDict setValue:self.internalBaseClassDescription forKey:kDetailDescription];
    [mutableDict setValue:self.iDProperty forKey:kDetailID];
    [mutableDict setValue:self.sub forKey:kDetailSub];
    [mutableDict setValue:self.listedDate forKey:kDetailListedDate];
    [mutableDict setValue:self.contract forKey:kDetailContract];
    [mutableDict setValue:self.address forKey:kDetailAddress];
    [mutableDict setValue:self.main forKey:kDetailMain];
NSMutableArray *tempArrayForFiles = [NSMutableArray array];
    for (NSObject *subArrayObject in self.files) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForFiles addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForFiles addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForFiles] forKey:@"kDetailFiles"];
    [mutableDict setValue:self.price forKey:kDetailPrice];
    [mutableDict setValue:self.title forKey:kDetailTitle];
NSMutableArray *tempArrayForPropertyInfo = [NSMutableArray array];
    for (NSObject *subArrayObject in self.propertyInfo) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForPropertyInfo addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForPropertyInfo addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForPropertyInfo] forKey:@"kDetailPropertyInfo"];
    [mutableDict setValue:[self.map dictionaryRepresentation] forKey:kDetailMap];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.auction = [aDecoder decodeObjectForKey:kDetailAuction];
    self.internalBaseClassDescription = [aDecoder decodeObjectForKey:kDetailDescription];
    self.iDProperty = [aDecoder decodeObjectForKey:kDetailID];
    self.sub = [aDecoder decodeObjectForKey:kDetailSub];
    self.listedDate = [aDecoder decodeObjectForKey:kDetailListedDate];
    self.contract = [aDecoder decodeObjectForKey:kDetailContract];
    self.address = [aDecoder decodeObjectForKey:kDetailAddress];
    self.main = [aDecoder decodeObjectForKey:kDetailMain];
    self.files = [aDecoder decodeObjectForKey:kDetailFiles];
    self.price = [aDecoder decodeObjectForKey:kDetailPrice];
    self.title = [aDecoder decodeObjectForKey:kDetailTitle];
    self.propertyInfo = [aDecoder decodeObjectForKey:kDetailPropertyInfo];
    self.map = [aDecoder decodeObjectForKey:kDetailMap];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_auction forKey:kDetailAuction];
    [aCoder encodeObject:_internalBaseClassDescription forKey:kDetailDescription];
    [aCoder encodeObject:_iDProperty forKey:kDetailID];
    [aCoder encodeObject:_sub forKey:kDetailSub];
    [aCoder encodeObject:_listedDate forKey:kDetailListedDate];
    [aCoder encodeObject:_contract forKey:kDetailContract];
    [aCoder encodeObject:_address forKey:kDetailAddress];
    [aCoder encodeObject:_main forKey:kDetailMain];
    [aCoder encodeObject:_files forKey:kDetailFiles];
    [aCoder encodeObject:_price forKey:kDetailPrice];
    [aCoder encodeObject:_title forKey:kDetailTitle];
    [aCoder encodeObject:_propertyInfo forKey:kDetailPropertyInfo];
    [aCoder encodeObject:_map forKey:kDetailMap];
}


@end
